package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.domain.Role;
import com.example.moviecms2.repository.MovieRepository;
import com.example.moviecms2.utils.ResultVOUtils;
import com.example.moviecms2.vo.MovieVO;
import com.example.moviecms2.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Rest风格电影控制器
 */
@RestController
@RequestMapping("/api/movie")
public class MovieRestController {

    @Autowired
    private MovieRepository movieRepository;

    /**
     * 查询电影列表
     * @url /api/movie/lists
     * @return
     */
    @RequestMapping(value = "/lists", method = RequestMethod.GET)
    public ResultVO<List<MovieVO>> lists(){
        List<Movie> list = movieRepository.findAll();

        List<MovieVO> voList = new ArrayList<>();
        for (Movie movie: list) {
            MovieVO vo = new MovieVO();
            vo.setId(movie.getId());
            vo.setName(movie.getName());
            vo.setPhoto(movie.getPhoto());
            voList.add(vo);

        }

//        ResultVO resultVO = new ResultVO();
//        resultVO.setCode(200);
//        resultVO.setMsg("success");
//        resultVO.setData(voList);
//        return resultVO;

        return ResultVOUtils.success(voList);

    }

    /**
     * 执行保存操作，然后重定向到列表页面 index.html
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultVO<MovieVO> doAdd(@RequestParam String name, @RequestParam  String photo, @RequestParam  String createDate){


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {

        }




        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        Movie result = movieRepository.save(movie);

        if (result != null){

            return ResultVOUtils.success(new MovieVO(result));
        }else{
            return ResultVOUtils.fail();
        }


    }

    /**
     * 根据id查询一个电影
     * @url /api/movie/{id}
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResultVO<MovieVO> detail(Model model, @PathVariable Long id){
        Movie movie = movieRepository.findOne(id);
        MovieVO vo = new MovieVO(movie);

//        ResultVO resultVO = new ResultVO();
//        resultVO.setCode(200);
//        resultVO.setMsg("success");
//        resultVO.setData(vo);
//        return resultVO;
        return ResultVOUtils.success(vo);
    }

    /**
     * 根据ID更新电影信息
     * @url /api/movie/{id}
     * @param id
     * @param name
     * @param photo
     * @param createDate
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String doEdit(@PathVariable Long id, @RequestParam String name,@RequestParam String photo,@RequestParam String createDate){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {
        }

        //1.通过id获取数据库的movie对象
        Movie movie = movieRepository.findOne(id);
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);

        //2.通过调用数据访问接口更新数据
        Movie result = movieRepository.save(movie);

        return result !=null ? "success":"fail";

    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResultVO<String> delete(@PathVariable Long id){
        //1.使用数据访问接口调用删除delete方法
        movieRepository.delete(id);

//        ResultVO<String> vo = new ResultVO<>();
//
//        vo.setCode(200);
//        vo.setMsg("success");
//        vo.setData(null);
//        return vo;

        return ResultVOUtils.success();
    }
}
